home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / MATRIX04.ARJ / MATCREAT.C < prev    next >
Text File  |  1992-05-25  |  4KB  |  196 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:    matcreat.c
  4. *    desc:    matrix mathematics - object creation
  5. *    by:    ko shu pui, patrick
  6. *    date:    24 nov 91 v0.1
  7. *    revi:    14 may 92 v0.2
  8. *        21 may 92 v0.3
  9. *    ref:
  10. *       [1] Mary L.Boas, "Mathematical Methods in the Physical Sciene,"
  11. *    John Wiley & Sons, 2nd Ed., 1983. Chap 3.
  12. *
  13. *    [2] Kendall E.Atkinson, "An Introduction to Numberical Analysis,"
  14. *    John Wiley & Sons, 1978.
  15. *
  16. *-----------------------------------------------------------------------------
  17. */
  18. #include <stdio.h>
  19.  
  20. #ifdef    __TURBOC__
  21. #include <alloc.h>
  22. #else
  23. #include <malloc.h>
  24. #endif
  25.  
  26. #include "matrix.h"
  27.  
  28. MATRIX    _mat_creat( row, col )
  29. int row, col;
  30. {
  31.     MATBODY    *mat;
  32.     int     i, j;
  33.  
  34.     if ((mat = (MATBODY *)malloc( sizeof(MATHEAD) + sizeof(double *) * row)) == NULL)
  35.         return (mat_error( MAT_MALLOC ));
  36.  
  37.     for (i=0; i<row; i++)
  38.     {
  39.     if ((*((double **)(&mat->matrix) + i) = (double *)malloc(sizeof(double) * col)) == NULL)
  40.         return (mat_error( MAT_MALLOC ));
  41.     }
  42.  
  43.     mat->head.row = row;
  44.     mat->head.col = col;
  45.  
  46.     return (&(mat->matrix));
  47. }
  48.  
  49. /*
  50. *-----------------------------------------------------------------------------
  51. *    funct:    mat_creat
  52. *    desct:    create a matrix
  53. *    given:  row, col = dimension, type = which kind of matrix
  54. *    retrn:    allocated matrix (use mat_free() to free memory)
  55. *-----------------------------------------------------------------------------
  56. */
  57. MATRIX    mat_creat( row, col, type )
  58. int row, col, type;
  59. {
  60.     MATRIX    A;
  61.  
  62.     if ((A =_mat_creat( row, col )) != NULL)
  63.         {
  64.         return (mat_fill(A, type));
  65.         }
  66.     else
  67.         return (NULL);
  68. }
  69.  
  70. /*
  71. *-----------------------------------------------------------------------------
  72. *    funct:    mat_fill
  73. *    desct:    form a special matrix
  74. *    given:  A = matrix, type = which kind of matrix
  75. *    retrn:    A
  76. *-----------------------------------------------------------------------------
  77. */
  78. MATRIX mat_fill( A, type )
  79. MATRIX A;
  80. int type;
  81. {
  82.     int    i, j;
  83.  
  84.     switch (type)
  85.         {
  86.         case UNDEFINED:
  87.             break;
  88.         case ZERO_MATRIX:
  89.         case UNIT_MATRIX:
  90.             for (i=0; i<MatRow(A); i++)
  91.             for (j=0; j<MatCol(A); j++)
  92.                 {
  93.                 if (type == UNIT_MATRIX)
  94.                     {
  95.                     if (i==j) A[i][j] = 1.0;
  96.                     continue;
  97.                     }
  98.                 A[i][j] = 0.0;
  99.                 }
  100.             break;
  101.         }
  102.     return (A);
  103. }
  104.  
  105.  
  106. /*
  107. *-----------------------------------------------------------------------------
  108. *    funct:    mat_free
  109. *    desct:    free an allocated matrix
  110. *    given:  A = matrix
  111. *    retrn:    nothing <actually 0 = NULL A passed, 1 = normal exit>
  112. *-----------------------------------------------------------------------------
  113. */
  114. int mat_free( A )
  115. MATRIX A;
  116. {
  117.     int i;
  118.  
  119.     if (A == NULL)
  120.         return (0);
  121.     for (i=0; i<MatRow(A); i++)
  122.         {
  123.         free( A[i] );
  124.         }
  125.     free( Mathead(A) );
  126.     return (1);
  127. }
  128.  
  129. /*
  130. *-----------------------------------------------------------------------------
  131. *    funct:    mat_copy
  132. *    desct:    duplicate a matrix
  133. *    given:    A = matrice to duplicated
  134. *    retrn:    C = A
  135. *    comen:
  136. *-----------------------------------------------------------------------------
  137. */
  138. MATRIX mat_copy( A )
  139. MATRIX A;
  140. {
  141.     int    i, j;
  142.     MATRIX    C;
  143.  
  144.     if ((C = mat_creat( MatRow(A), MatCol(A), UNDEFINED )) == NULL)
  145.         return (NULL);
  146.  
  147.     for (i=0; i<MatRow(A); i++)
  148.     for (j=0; j<MatCol(A); j++)
  149.         {
  150.         C[i][j] = A[i][j];
  151.         }
  152.     return (C);
  153. }
  154.  
  155.  
  156. MATRIX mat_colcopy1( A, B, cola, colb )
  157. MATRIX A, B;
  158. int cola, colb;
  159. {
  160.     int    i, n;
  161.  
  162.     n = MatRow(A);
  163.     for (i=0; i<n; i++)
  164.         {
  165.         A[i][cola] = B[i][colb];
  166.         }
  167.     return (A);
  168. }
  169.  
  170. int fgetmat( A, fp )
  171. MATRIX A;
  172. FILE *fp;
  173. {
  174.     int     i, j, k=0;
  175.  
  176.     for (i=0; i<MatRow(A); i++)
  177.     for (j=0; j<MatCol(A); j++)
  178.         {
  179. /*
  180. *    to avoid a bug in TC
  181. */
  182. #ifdef    __TURBOC__
  183.         {
  184.         double    temp;
  185.         k += fscanf( fp, "%lf", &temp );
  186.         A[i][j] = temp;
  187.         }
  188. #else
  189.         k += fscanf( fp, "%lf", &A[i][j] );
  190. #endif
  191.  
  192.         }
  193.  
  194.     return (k);
  195. }
  196.